Chapter 2.2 - Data representations for neural networks


In [1]:
# importing numpy
import numpy as np

Scalar (0-D tensor)


In [2]:
x = np.array(10)

In [3]:
x.ndim


Out[3]:
0

In [4]:
x.shape


Out[4]:
()

Vector (1-D tensor)


In [5]:
x = np.array([12, 13, 14])

In [6]:
x


Out[6]:
array([12, 13, 14])

In [7]:
x.ndim


Out[7]:
1

In [8]:
x.shape


Out[8]:
(3,)

Matrices (2-D tensor)


In [9]:
x = np.array([[1,2,3],
             [4,5,6]])

In [10]:
x.ndim


Out[10]:
2

In [11]:
x.shape


Out[11]:
(2, 3)

3-D tensors and higher-dimensional tensors


In [12]:
x = np.array([[[5, 78, 2, 34, 0],
               [6, 79, 3, 35, 1],
               [7, 80, 4, 36, 2]],
              [[5, 78, 2, 34, 0],
               [6, 79, 3, 35, 1],
               [7, 80, 4, 36, 2]],
              [[5, 78, 2, 34, 0],
               [6, 79, 3, 35, 1],
               [7, 80, 4, 36, 2]]])

In [13]:
x.ndim


Out[13]:
3

In [14]:
x.shape


Out[14]:
(3, 3, 5)